import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// ElementPlus的Icon自动导入
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'

// 引入path
import path from 'path'
// 引入自定义icon插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default defineConfig(({mode}) => {
  const env = loadEnv(mode, process.cwd()); 
  return {
      plugins: [
        vue(),
        vueDevTools(),
        // 配置自定义icon
        createSvgIconsPlugin({
          iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/svg')],
          symbolId: '[name]'
        }),
        AutoImport({
          resolvers: [
            ElementPlusResolver(),
            // 自动导入图标
            IconsResolver({
              prefix: 'Icon',
            }),
          ]
        }),
        Components({
          resolvers: [
            ElementPlusResolver(),
            // 自动注册图标
            IconsResolver({
              enabledCollections: ['ep'],
            }),
          ]
        }),
        // 自动安装图标
        Icons({
          autoInstall: true,
        }),
      ],
      resolve: {
        alias: {
          '@': fileURLToPath(new URL('./src', import.meta.url))
        }
      },
      server: {
        proxy: {
            '/api': {   // 获取路径中包含了/api的请求
                //target: 'http://192.168.1.51:8999',        // 服务端地址
                target: env.VITE_TARGET_URL,
                changeOrigin: true, // 修改源
                rewrite:(path) => path.replace(/^\/api/, '')   // api 替换为 ''
            }
        },
        host: "0.0.0.0"  // 局域网其他电脑可访问
      }
}
})
